<?php


namespace app\user\controller;


use app\common\controller\Common;
use app\common\model\Merchant;
use think\Db;
use think\facade\Log;

class Verification extends Common
{
    public function memberverf()
    {

        $openid = request()->param('openid');
        $memid = request()->param('memid');
        $key=\request()->param('key');
        $cate_id = request()->param('cate_id');
        $area_id = request()->param('area_id');
        $discount = request()->param('discount');

        if ($openid == "" || $openid == null) return $this->errJson('缺少参数');
        if ($memid == "" || $memid == null) return $this->errJson('缺少参数');
        $data=[];
        if(!is_null($cate_id)&&$cate_id!="") $data['cate_id']=$cate_id;
        if(!is_null($area_id)&&$area_id!=null) $data['area_id']=$area_id;
        if(!is_null($discount)&&$discount!=null) $data['discount']=$discount;
        $where ="";
        if (count($data)>0){
            foreach ($data as $ke=>$v){
                if ($ke=='cate_id') $where.=" and cate_id in(".$v.")";
                if($ke=='discount') $where.=" and discount between ".$v[0]." and ".$v[1];
                if($ke=='area_id') $where.=" and area_id in(".$v.")";
            }
        }

        //还有问题，需要完善
        $page = $this->getPage();
        $pagesize = $this->getPageSize();
        $min = ($page - 1) * $pagesize;
        $max = $pagesize;
        $reverf = Db::query("SELECT merid,mer_name,mer_img,area_id,cate_id,cate_name FROM verf_record WHERE memid=".$memid.$where);
        $sql="SELECT merid,mer_name,mer_img,area_id,cate_id,cate_name,discount,mer_address,FROM_UNIXTIME(time,'%Y-%m-%d %H:%i') as time FROM verf_record WHERE memid=".$memid.$where." limit " . $min . "," . $max;
        $verf = Db::query($sql);
        if (count($verf) == 0) return $this->errJson('暂无返回记录');
        return $this->listJson($verf, count($reverf));
    }

    //筛选
    public function screenverf()
    {
        $memid = \request()->param('memid');
        $retscreen = Db::query("SELECT verf.cate_name as title,verf.cate_id as cate_id, ar.area_title as area,verf.area_id FROM verf_record as verf JOIN area as ar on verf.area_id=ar.area_id where memid=" . $memid);
        if (count($retscreen) == 0) return $this->errJson('暂无数据');

        foreach ($retscreen as $key=>$value){
            $title[$key]['title'] =$value['title'];
            $title[$key]['cate_id'] =$value['cate_id'];
            $area[$key]['area'] =$value['area'];
            $area[$key]['area_id'] =$value['area_id'];
            $titles=$title;
            $areas=$area;
        }

        $conut = count($titles);//去重
        $rettile=[];
        for ($i=0;$i<$conut;$i++){
            $a=$titles[$i];
            unset($titles[$i]);
            if (!in_array($a,$titles)){
                $rettile[]=$a;
            }
        }
        $conut1 = count($areas);//去重
        $retarea=[];
        for ($i=0;$i<$conut1;$i++){
            $a=$areas[$i];
            unset($retarea[$i]);
            if (!in_array($a,$retarea)){
                $retarea[]=$a;
            }
        }

        $data=[
            'title'=>array_values($rettile),
            'area'=>array_values($retarea)
        ];
        return $this->okJson('ok', $data);
    }

    //服务详情
    public function show()
    {
        $merid = request()->param('merid');
        $merid = \app\common\model\Merchant::get($merid);
        if ($merid) return $this->okJson('ok', $merid);

        return $this->errJson('暂无数据');
    }
}